﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _16.TasksSequence
{
    class Graph
    {
        private List<int>[] childNodes;
        private List<int> sequencePath;

        public Graph(List<int>[] childNodes)
        {
            this.childNodes = childNodes;
            this.sequencePath = new List<int>();
        }

        public int NodesCount
        {
            get { return childNodes.Length; }
        }

        private void PrintSequence()
        {
            StringBuilder sb = new StringBuilder(sequencePath.Count);
            foreach (var node in sequencePath)
            {
                sb.Append(node + 1);
                sb.Append(", ");
            }
            Console.WriteLine(sb.ToString().TrimEnd(',', ' '));
        }

        public void FindSequenceOfAllNodes()
        {
            for (int i = 1; i < childNodes.Length; i++)
            {
                if (HasCycle(i, i, 1, new bool[childNodes.Length]))
                {
                    Console.WriteLine("Sequence not possible!");
                    return;
                }
            }

            bool[] visited = new bool[childNodes.Length];
            foreach (var node in FindStartNodes())
            {
                RecursiveDFS(node, visited);
            }

            PrintSequence();
        }

        private bool HasUnvisitedParents(int node, bool[] visited)
        {
            for (int childList = 1; childList < childNodes.Length; childList++)
            {
                if (childNodes[childList] == null || childNodes[childList].Count == 0)
                {
                    continue;
                }
                if (childNodes[childList].Contains(node) && !visited[childList])
                {
                    return true;
                }
            }
            return false;
        }

        private List<int> FindStartNodes()
        {
            List<int> startNodes = new List<int>();
            for (int node = 1; node < childNodes.Length; node++)
            {
                bool found = true;
                foreach (var childsList in childNodes)
                {
                    if (childsList == null || childsList.Count == 0)
                    {
                        continue;
                    }
                    if (childsList.Contains(node))
                    {
                        found = false;
                    }
                }
                if (found)
                {
                    startNodes.Add(node);
                }
            }

            return startNodes;
        }

        private void RecursiveDFS(int node, bool[] visited)
        {
            if (HasUnvisitedParents(node, visited))
            {
                return;
            }
            visited[node] = true;
            sequencePath.Add(node);
            foreach (var childNode in this.childNodes[node])
            {
                if (!visited[childNode])
                {
                    RecursiveDFS(childNode, visited);
                }
            }
        }

        private bool HasCycle(int current, int startNode, int steps, bool[] visited)
        {
            visited[current] = true;
            foreach (var node in childNodes[current])
            {
                if (node == startNode && steps > 3)
                {
                    return true;
                }
                else if (!visited[node])
                {
                    if (HasCycle(node, startNode, steps + 1, visited))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Graph graph = new Graph(ReadGraph());

            graph.FindSequenceOfAllNodes();
        }

        static List<int>[] ReadGraph()
        {
            // Read number of nodes N
            string input = Console.ReadLine();
            int N = int.Parse(input);

            // Read graph as adjacency list
            List<int>[] graph = new List<int>[N];

            for (int i = 0; i < N; i++)
            {
                graph[i] = new List<int>();

                input = Console.ReadLine();

                if (input == Environment.NewLine)
                {
                    continue;
                }

                string[] childs = input.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                foreach (var child in childs)
                {
                    graph[i].Add(int.Parse(child) - 1);
                }
            }

            return graph;
        }
    }
}
